package com.cjk.controller;

import com.cjk.pojo.Clazz;
import com.cjk.pojo.ClazzPageHelper;
import com.cjk.pojo.PageResult;
import com.cjk.pojo.Result;
import com.cjk.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result page(ClazzPageHelper clazzPageHelper) {
        log.info("分页查询所有班级");
        PageResult<Clazz> clazzList = clazzService.page(clazzPageHelper);
        return Result.success(clazzList);
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id删除班级：{} " ,id);
        clazzService.deleteById(id);
        return Result.success();
    }

    /**
     * 添加班级
     * @param clazz
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        log.info("添加班级：{} " ,clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    /**
     * 根据id查询班级
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询班级，id：{}",id);
        Clazz clazz = clazzService.findById(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级
     * @param clazz
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级信息：{}",clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /**
     * 查询所有班级
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有班级");
        List<Clazz> clazzList = clazzService.list();
        return Result.success(clazzList);
    }


}
